package com.hwd.srb.sms.controller.api;

import com.hwd.srb.common.constant.SmsConstants;
import com.hwd.srb.common.exception.Assert;
import com.hwd.srb.common.result.R;
import com.hwd.srb.common.result.ResponseEnum;
import com.hwd.srb.common.util.RegexValidateUtils;
import com.hwd.srb.sms.client.CoreUserInfoClient;
import com.hwd.srb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 黄伟东/Victor
 * @date 2022/3/16 1:12
 * @Description
 */
@RestController
@RequestMapping("/api/sms")
@ApiModel("后台短信测试接口")
//@CrossOrigin //跨域
@Api(tags = "短信测试接口")
@Slf4j
public class ApiSmsController {

    @Resource
    private SmsService smsService;

    @Resource
    private CoreUserInfoClient coreUserInfoClient;

    @ApiOperation("流量测试接口")
    @GetMapping("/test")
    public R test(){
        boolean b = coreUserInfoClient.checkMobile("15089822215");

        return b ? R.ok() : R.error();
    }


    /**
     * 发送验证码短信
     *
     * @param mobile 手机号
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation("验证码短信发送接口")
    @GetMapping("/send/{mobile}")
    public R send(@ApiParam(value = "手机号", required = true) @PathVariable("mobile") String mobile) {
        // 验证手机号号
        boolean bMobile = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(bMobile, ResponseEnum.MOBILE_ERROR);

        // 调用core服务，检测手机号是否已被注册
        boolean bIfMobile = coreUserInfoClient.checkMobile(mobile);
        Assert.isTrue(!bIfMobile, ResponseEnum.MOBILE_EXIST_ERROR);

        // 调用业务层方法发送短信
        smsService.send(mobile);
        return R.ok().message(SmsConstants.SEND_SMS_SUCCESS);
    }
}
